/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logger;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

/**
 *
 * @author Kvest
 */
public class LoggerForm extends Form implements CommandListener {
    private LoggerFormListener listener;
    
    public LoggerForm(LoggerFormListener listener) {
        super("Logger");
        
        this.listener = listener;
        
        setContent();
        
        Command exit = new Command("Exit", Command.EXIT, 0);
        addCommand(exit);
        Command clear = new Command("Clear", Command.CANCEL, 0);
        addCommand(clear);
        setCommandListener(this);
    }

    private void setContent() {
        deleteAll();
        
        append(Logger.getInstance().getLog());
    }
    
    public void commandAction(Command c, Displayable d) {
        if (c.getCommandType() == Command.EXIT && listener != null) {
            listener.onBack();
        } else if (c.getCommandType() == Command.CANCEL) {
            Logger.getInstance().clear();
            setContent();
        }
    }
    
    public interface LoggerFormListener {
        public void onBack();
    }
}
